<template>
  <div class="app-container">
    <el-row style="text-align: right;margin-bottom: 15px"><el-button
      type="primary"
      icon="el-icon-plus"
      size="mini"
      @click="handleAdd"
    >添加</el-button></el-row>
      <el-row>
        <el-col :span="4">
          <div class="grid-content bg-purple" :style="'height:'+boxheight">
            <div class="above firstAbove"><div class="title">食材分类</div></div>
            <div class="under first_under">
              <ul>
                <li v-for="(dict,index) in dict.type.food_type" @click="changefoodSort(dict.value,dict.label)" :class="actived==dict.value?'chooseStyle':''">
                  {{dict.label}}
                </li>
              </ul>
            </div>
          </div>
        </el-col>
        <el-col :span="6" style="padding:0 10px">
          <div class="grid-content bg-purple" :style="'height:'+boxheight">
            <div class="above">
              <div class="title" style="width: 100%;position: relative;text-align: center;">食材列表
              </div>
            </div>
            <div class="second_under">
              <ul>
                <li v-for="(item,index) in foodList" >
                  <div class="left" @click="changefoodList(item.foodId,index)" :class="listactived==index?'chooseStyle':''">
                    {{item.foodName}}
                  </div>
                  <div v-if="item.type==1" class="right" @click="delfoodname(item.foodId)">
                    删除
                  </div>
                </li>
              </ul>
            </div>
          </div>
        </el-col>
        <el-col :span="14">
          <div class="grid-content bg-purple"  v-show="formopen">
            <el-form ref="form" :model="form" :rules="rules" label-width="100px" >
              <div class="third_above">
                <div class="title">食材信息</div>
                <div class="special_Input">
                  <el-form-item label="食材图片：" prop="foodImage">
                    <imageUpload v-model="form.foodImage" :limit="1" />
                  </el-form-item>
                  <el-form-item label="名称：" prop="foodName">
                    <el-input
                      v-model="form.foodName"
                      size="small"
                    />
                  </el-form-item>
                  <el-form-item label="类别：" prop="parentId">
                    <el-select v-model="form.parentId" >
                      <el-option
                        v-for="dict in dict.type.food_type"
                        :key="dict.value"
                        :label="dict.label"
                        :value="parseInt(dict.value)"
                        size="small">
                      </el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="重量：" prop="weight">
                    <el-input v-model="form.weight" type="number">
                      <template slot="append">克（g）</template>
                    </el-input>
                  </el-form-item>
                </div>
              </div>
              <div class="third_under" :style="'height:'+unheight">
                <div class="one_part">
                  <div class="head">主要营养：</div>
                  <div class="info">
                    <div class="each">
                      <el-form-item label="能量" prop="energy">
                        <el-input v-model="form.energy" type="number"/>
                      </el-form-item>
                      <div class="foot">千卡（Kcal）</div>
                    </div>
                    <div class="each">
                      <el-form-item label="脂肪" prop="fat">
                        <el-input v-model="form.fat" type="number"/>
                      </el-form-item>
                    </div>
                    <div class="each">
                      <el-form-item label="蛋白质" prop="protein">
                        <el-input v-model="form.protein" type="number"/>
                      </el-form-item>
                    </div>
                    <div class="each">
                      <el-form-item label="膳食纤维" prop="fatFiber">
                        <el-input v-model="form.fatFiber" type="number"/>
                      </el-form-item>
                    </div>
                    <div class="each">
                      <el-form-item label="碳水化合物" prop="carbohydrate">
                        <el-input v-model="form.carbohydrate" type="number"/>
                      </el-form-item>
                      <div class="foot">千卡（Kcal）</div>
                    </div>
                  </div>
                </div>

                <div class="one_part">
                  <div class="head">维生素：</div>
                  <div class="info">
                    <div class="each">
                      <el-form-item label="维生素A" prop="vitaminA">
                        <el-input v-model="form.vitaminA" type="number"/>
                      </el-form-item>
                    </div>
                    <div class="each">
                      <el-form-item label="胡萝卜素" prop="carotene">
                        <el-input v-model="form.carotene" type="number"/>
                      </el-form-item>
                    </div>
                    <div class="each">
                      <el-form-item label="维生素b1" prop="vitaminB1">
                        <el-input v-model="form.vitaminB1" type="number"/>
                      </el-form-item>
                    </div>
                    <div class="each">
                      <el-form-item label="维生素b2" prop="vitaminB2">
                        <el-input v-model="form.vitaminB2" type="number"/>
                      </el-form-item>
                    </div>
                    <div class="each">
                      <el-form-item label="维生素b3" prop="vitaminB3">
                        <el-input v-model="form.vitaminB3" type="number"/>
                      </el-form-item>
                    </div>
                    <div class="each">
                      <el-form-item label="维生素c" prop="vitaminC">
                        <el-input v-model="form.vitaminC" type="number"/>
                      </el-form-item>
                    </div>
                    <div class="each">
                      <el-form-item label="维生素e" prop="vitaminE">
                        <el-input v-model="form.vitaminE" type="number"/>
                      </el-form-item>
                    </div>
                  </div>
                </div>

                <div class="one_part">
                  <div class="head">矿物质：</div>
                  <div class="info">
                    <div class="each">
                      <el-form-item label="钙" prop="ca">
                        <el-input v-model="form.ca" type="number"/>
                      </el-form-item>
                    </div>
                    <div class="each">
                      <el-form-item label="镁" prop="mg">
                        <el-input v-model="form.mg" type="number"/>
                      </el-form-item>
                    </div>
                    <div class="each">
                      <el-form-item label="铁" prop="fe">
                        <el-input v-model="form.fe" type="number"/>
                      </el-form-item>
                    </div>
                    <div class="each">
                      <el-form-item label="锌" prop="zn">
                        <el-input v-model="form.zn" type="number"/>
                      </el-form-item>
                    </div>
                    <div class="each">
                      <el-form-item label="硒" prop="se">
                        <el-input v-model="form.se" type="number"/>
                      </el-form-item>
                    </div>
                    <div class="each">
                      <el-form-item label="铜" prop="cu">
                        <el-input v-model="form.cu" type="number"/>
                      </el-form-item>
                    </div>
                    <div class="each">
                      <el-form-item label="锰" prop="mn">
                        <el-input v-model="form.mn" type="number"/>
                      </el-form-item>
                    </div>
                    <div class="each">
                      <el-form-item label="碘" prop="i">
                        <el-input v-model="form.i" type="number"/>
                      </el-form-item>
                    </div>
                  </div>
                </div>
                <div slot="footer" class="control">
                  <el-button @click="cancel">取 消</el-button>
                  <el-button type="primary" @click="submitForm">确 定</el-button>
                </div>
              </div>
            </el-form>

          </div>
        </el-col>
      </el-row>
  </div>
</template>

<script>

import { addlistfood, dellistfood, listfood,updateFood } from '@/api/base/grow'

export default {
  name: "Ingredients",
  dicts: ['food_type'],
  data() {
    return {
      boxheight:0,
      unheight:0,
      actived:null,//分类激活
      listactived:null,//列表激活
      foodList:[],
      form:{},
      rules:{
        foodName: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        parentId: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        foodId: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        foodImage:[
          {
            required: true, message: "不能为空", trigger: "blur"
          }
        ]
      },
      open:false,//是否添加窗口
      formopen:false,//是否显示右侧详细信息
    };
  },
  created() {
  },
  mounted() {
    //获取屏幕高度
    let that=this
    that.boxheight = document.documentElement.clientHeight - 164 + "px;";
    that.unheight=document.documentElement.clientHeight - 340 + "px;";
    window.onresize = function temp() {
      that.boxheight = document.documentElement.clientHeight - 164 + "px;";
      that.unheight=document.documentElement.clientHeight - 340 + "px;";
    };
  },
  methods: {
    /**点击食材分类*/
    changefoodSort(value,name){
      this.actived=value
      console.log(value)
      //调用食材列表接口
      this.getList()

    },
    /**食材列表*/
    async getList(){
      try {
        let params={
          parentId:this.actived
        }
        const {data} = await listfood(params)
        console.log(data)
        this.foodList=data
      }catch (e) {
        console.log(e)
      }
    },
    /**点击食材列表*/
    changefoodList(foodId,index){
      this.listactived=index
      this.formopen=true
      this.open=false
      this.form=this.foodList.find(item=>item.foodId==foodId)
    },
    /**删除食材列表**/
    async delfoodname(foodId){
      try {
        const data=await dellistfood(foodId)
        if(data.code==200){
          this.msgSuccess("删除成功");
          this.getList()
        }else{
          this.msgSuccess(data.msg);
        }
      }catch (e) {
        console.log(e)
      }
    },
    /**新增*/
    handleAdd(){
      this.reset();
      this.formopen=true
      this.open=true
    },
    /**新增*/
    submitForm(){
      this.openLoading();
      this.$refs["form"].validate(valid=>{
        if(valid){
          if(!this.open){
            updateFood(this.form).then(res=>{
              if(res.code==200){
                this.msgSuccess("修改成功");
                this.getList()
              }else{
                this.msgSuccess(res.msg);
              }
            })
          }else {
            addlistfood(this.form).then(res=>{
              if(res.code==200){
                this.msgSuccess("新增成功");
                this.getList()
              }else{
                this.msgSuccess(res.msg);
              }
            })
          }

        }
        this.loading.close();
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        foodName: null,
        type: 1,
        parentId: null,
        weight: null,
        foodImage:null,
        energy: null,
        fat: null,
        protein: null,
        fatFiber: null,
        carbohydrate: null,
        vitaminA: null,
        carotene: null,
        vitaminB1: null,
        vitaminB2: null,
        vitaminB3: null,
        vitaminC: null,
        vitaminE: null,
        ca: null,
        mg: null,
        fe: null,
        zn: null,
        se: null,
        cu: null,
        mn: null,
        i: null,
      };
      this.resetForm("form");
    },
    /**关闭*/
    cancel(){
      this.open = false;
      this.formopen=false
      this.reset();
    },
    // 打开加载层
    openLoading() {
      this.loading = this.$loading({
        lock: true,
        text: "拼命读取中",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
    },
  }
};
</script>

<style lang="scss" scoped>
::v-deep .el-upload__tip{
  display: none;
}
ol, ul {
  list-style: none;
  margin: 0;
  padding: 0;
}
.app-container{
  .grid-content{
    border: 1px solid #ccc;
    border-radius: 5px;
    .above,.third_above{
      border-bottom: 1px solid #ccc;
      font-weight: 700;
      font-style: normal;
      font-size: 18px;
      display: flex;
      -webkit-box-orient: vertical;
      -webkit-box-direction: normal;
      -ms-flex-direction: column;
      flex-direction: column;
      -webkit-box-align: center;
      -ms-flex-align: center;
      align-items: center;
      justify-content: center;
      height: 14%;
      .title{
        margin: 20px 0;
      }
    }
    .first_under,.second_under{
      height: 86%;
      overflow: auto;
    }

    .under ul{
      padding: 10px;
      overflow: auto;
      li{
        font-size: 14px;
        padding: 12px 0;
        cursor: pointer;
      }
      .chooseStyle{
        color: #fff;
        background-color: #f60;
      }
    }
    .second_under ul{
      padding: 10px;
      li{
        font-size: 14px;
        cursor: pointer;
        display: -webkit-box;
        display: -ms-flexbox;
        display: flex;
        -webkit-box-pack: justify;
        -ms-flex-pack: justify;
        justify-content: space-between;
        .left{
          width: 66%;
          padding: 12px 0;
          padding-left: 10px;
        }
        .right{
          width: 30%;
          padding: 12px 0;
          text-align: center;
          color: #f60;
        }
      }
      .chooseStyle{
        color: #fff;
        background-color: #f60;
      }
    }
    .third_above{
      min-height: 120px;
      border-bottom: 1px solid #ccc;
      .special_Input{
        display: -webkit-box;
        display: -ms-flexbox;
        display: flex;
        -ms-flex-wrap: wrap;
        flex-wrap: wrap;
        padding: 0 10px;
      }
    }
    .third_under{
      padding: 20px;
      overflow: auto;
      .one_part{
        .head{
          font-size: 16px;
          margin-bottom: 15px;
        }
        .info{
          display: flex;
          flex-wrap: wrap;
          .each{
            width: 50%;
            display: -webkit-box;
            display: -ms-flexbox;
            display: flex;
            .foot{
              font-size: 14px;
              color: #606266;
              height: 40px;
              line-height: 40px;
              width: 108px;
            }
          }
        }
      }
      .control{
        text-align: right;
      }
    }
    .add-ingredient{
      position: absolute;
      right: 20px;
      bottom: 0;
      top: 0;
      margin: auto;
    }

  }
}
</style>

